package com.easybuilder.blog.dao;

import com.alibaba.fastjson.JSONObject;
import com.easybuilder.blog.entity.BlogLikes;
import com.easybuilder.common.base.dao.BaseDao;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.ExampleMapper;

import java.util.List;
import java.util.Map;


@Repository
public interface BlogLikesDao extends BaseDao<BlogLikes>, ExampleMapper<BlogLikes> {

    /** 获取当前作品最新的点赞数据量 */
    @Select("select sum(case when status in (1) then 1 else 0 end) as likes, sum(case when status in (0) then 1 else 0 end) dislikes from blog_likes where article_id = #{articleId} and type = #{type}")
    Map<String, Integer> sumLikes(Long articleId, Integer type);

    /** 批量获取一系列作品的点赞量和回踩量*/
    @Select("select article_id articleId, sum(case when status in (1) then 1 else 0 end) as likes, sum(case when status in (0) then 1 else 0 end) dislikes from blog_likes where type = #{type} and article_id in (${articleIds}) group by article_id")
    List<JSONObject> batchSumLikes(Integer type, String articleIds);

}